<?php
require_once("include/game_core.php");
require_once(USER_CONTROLLER_FILE);
require_once(DB_FILE);
require_once(UTILS_FILE);

if ( (! UserController::isLoggedIn()) ||
            (! UserController::isPlaying())  ) {
        header( 'Location: index.php' ) ;
    }
    
//////////////////////////////
//      getOpponnentID      //
//////////////////////////////
    function getOpponnent($uid, $game_id) {
        
        $query = "SELECT id, nick FROM users
                WHERE id <> ? AND game_id=?";

        $db = new DBConnection();
        if ($db->executeBindedQuery($query,"ii",$uid, $game_id)) {
            $result = $db->getResult();
            if( ($result != null) && ($result->num_rows > 0) ) {
                $row = $result->fetch_assoc();
            }
            else {
                Utils::Error("ERROR in Query");
            }
        }
        if(isset($row)) {
                return $row;
        }
        return null;    
    }
/////////////////////////
//      getGameId      //
/////////////////////////
    function getGameId($uid) {
        $query = "SELECT game_id FROM users
                WHERE `id` = $uid 
                LIMIT 1";

        $db = new DBConnection();
        if ($db->executeQuery($query)) {
            $result = $db->getResult();
            if( ($result != null) && ($result->num_rows == 1) ) {
                $row = $result->fetch_assoc();
            }
        }
        if(isset($row)) {
                return $row['game_id'];
        }
        return null;    
    }
//////////////////////////
//      getMessege      //
//////////////////////////
    function getMessege($op_id) {
        $query = "SELECT chat_str FROM chat
                WHERE `id` = ?";

        $db = new DBConnection();
        if ($db->executeBindedQuery($query,"i",$op_id)) {
            $result = $db->getResult();
            if( ($result != null) && ($result->num_rows > 0) ) {
                $rows = "";
                while($row = $result->fetch_assoc()) {
                    $rows .= $row['chat_str'] . "\n";
                  }
                return $rows;
            }
            else {
                 return null;
                
            }
        }
        else {
             return null;
        }
     }
//////////////////////////
//      removeMsg       //
//////////////////////////
    function removeMsg($op_id) {
        $query = "DELETE FROM chat
                WHERE id = $op_id";
        $db = new DBConnection();
        if (!$db->executeQuery($query)) {
            return "ERROR in DB";
        }
    }
    
/////////////////////////////////////////////////////
//                  script                         //
/////////////////////////////////////////////////////

    //get uid
    $uid = UserController::getUserId();
    if($uid == "") {
        echo "ERROR, cant get uid";
    }

    //get game_id
    $game_id = getGameId($uid);
    if($game_id == null) {
        return "ERROR in DB";
    }
    $result = getOpponnent($uid, $game_id);
    if($result == null) {
        return "ERROR in DB";
    }
    
    $op_id = $result['id'];
    $nick = $result['nick'];

    //find if has messege?
    $msg = getMessege($op_id);
    if($msg == null) {
        echo "";
    }
    else {
        removeMsg($op_id);
        echo $nick. ": " .$msg;
    }






?>
